#include "Globals.h"
#include "ISDKMesh.h"
#include "ITexture.h"

//--------------------------------------------------------------------------------
//	initialize an anim asset
//--------------------------------------------------------------------------------
void *ISDKMesh::AssetInit( IAssetFile *pi, const CMasterIndexEntry *pme, void *context )
{
	ISDKMesh	*ret = new ISDKMesh();

	if( ret )
	{
		UINT numBytes;
		BYTE *pData = (BYTE*)pi->GetChunk( pme->nameHash, pme->primaryChunk, &numBytes );
		if( FAILED( ret->Create( DXUTGetD3D9Device(), pData, numBytes ) ) )
		{
			delete ret;
			return NULL;
		}
	}
	return (void*)ret;
}

//--------------------------------------------------------------------------------
//	uninitialize an anim asset
//--------------------------------------------------------------------------------
void ISDKMesh::AssetUninit( void *ptr, void *context )
{
	ISDKMesh *pMesh = (ISDKMesh*)ptr;
	pMesh->Destroy();
	delete pMesh;
}

void CALLBACK BindTexture( IDirect3DDevice9* pDev, char* szFileName, IDirect3DTexture9** ppTexture, void* pContext )
{
	ITexture *pTex = (ITexture*)((CAssetFileMgr*)pContext)->FindAsset( szFileName, "TEXTURE" );
	if( pTex )
	{
		ppTexture = &pTex->pTexture;
	}
}

//--------------------------------------------------------------------------------
//	fixup an asset
//--------------------------------------------------------------------------------
void ISDKMesh::AssetFixup( CAssetFileMgr *pMgr, const IAssetFile *pFile, void *ptr, const CMasterIndexEntry *pEntry )
{
	ISDKMesh			*pMesh		= (ISDKMesh*)ptr;
	SDKMESH_CALLBACKS9	callback	= { BindTexture, NULL, NULL, pMgr };

	pMesh->FixupMesh( DXUTGetD3D9Device(), &callback );
}